/*
 * Description: 用来缓存一个调用，当调用返回时会找到相应的调用存入返回值，并且唤醒。
 */
#ifndef GUEST_LINK_CALL_RECORD
#define GUEST_LINK_CALL_RECORD

#include <mutex>
#include <condition_variable>
#include "guest2host.pb.h"
#include "error.h"

namespace COMMON {
class CallRecord {
public:
    /**
     * 传入装返回的指针，用来构造一个调用的记录
     */
    explicit CallRecord(google::protobuf::MessageLite *res);
    CallRecord(const CallRecord&) = delete;
    CallRecord& operator = (const CallRecord&) = delete;
    ~CallRecord();
    /**
     * 等待唤醒响应
     */
    void WaitForResponse();
    /**
     * 用来唤醒当前调用
     */
    void WakeUp();
    /**
     * 用来把返回值放置到构造时传入的指针中
     */
    void SetResult(const Guest::protobuf::link::Result &result);
    /**
     * 查询调用服务端返回的错误码
     */
    int GetErrorCode() const;

private:
    std::mutex m_mutex = {};
    std::condition_variable m_cv = {};
    google::protobuf::MessageLite *m_result = nullptr;
    int m_errorCode = COMMON::DEFAULT_ERROR; // 服务端返回错误码
    bool m_isWoken = false;
    bool m_isWait = false;
};
}
#endif // GUEST_LINK_CALL_RECORD
